function searchDeliveryDept() {
	var word = $("#search_word").val();
	
	$.ajax({
		url: contextPath + '/cm_common/list',
		data: {companyCode:'1000', majorCode:'TMO05', word:word},
		type: 'GET',
		dataType: 'json'
	}).done(function(data) {
		$('#dept_table').bootstrapTable('load', data);
	});
}

function initDeliveryDeptPopupUI() {
	var html = [];
	html.push('<div class="modal-header">');
	html.push('	<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>');
	html.push('	<h4 class="modal-title" id="my_modal_label">출고 부서</h4>');
	html.push('</div>');
	html.push('<div class="modal-body">');
	html.push(' <div class="form-inline">');
	html.push(' 	<div class="row">');
	html.push('			<div class="col-sm-6">');
	html.push('				<div class="form-group form-group-sm">');
	html.push('					<label class="control-label" for="search_word" style="width:100px">조회 조건</label>');
	html.push('					<input type="text" class="form-control" id="search_word" style="width:200px"></input>');	
	html.push('					<input type="button" class="btn btn-primary btn-sm" id="search_btn" value="검색" style="width:60px" onclick="searchDeliveryDept()"></input>');
	html.push('				</div>');
	html.push('			</div>');
	html.push('		</div>');
	html.push('	</div>');
	html.push('	<hr style="width:100%; height:1px; background-color:lightgray;"/>');
	html.push('	<table id="dept_table" data-click-to-select="true" data-height="300" data-pagination="true" data-search="false">');
	html.push('		<thead>');
	html.push('			<tr>');
	html.push('				<th data-radio="true"></th>');
	html.push('				<th data-field="minorCode">부서코드</th>');
	html.push('				<th data-field="minorName">부서명</th>');
	html.push('			</tr>');
	html.push('		</thead>');
	html.push('		<tbody>');
	html.push('		</tbody>');
	html.push('	</table>');
	html.push('</div>');
	html.push('<div class="modal-footer">');
	html.push('<button type="button" class="btn btn-primary" id="confirm">확인</button>');
	html.push('<button type="button" class="btn btn-default" data-dismiss="modal">취소</button>');
	html.push('</div>');

	$('div.modal-content').html(html.join(''));
	
	// bootstrapTable 적용
	$('#dept_table').bootstrapTable();

	$('#dept_table').dblclick(function(event) {
		var row = $(event.target).closest('tr');
		
		var selectedCode = row.find('td:nth(1)').text().trim();
		var selectedName = row.find('td:nth(2)').text().trim();
		
		$('#dept_code').val(selectedCode);
		$('#dept_name').val(selectedName);

		$('#modal_div').modal("hide");
	});

	
	$('#confirm').click(function() {
		var selectedCustomerCode = "";
		var selectedCustomerName = "";

		var trList = $('#dept_table tr');

		trList.each(function(i, e) {
			var checkFlag = $(e).find('input:radio').prop('checked');
			
			if ( checkFlag == true ) {
				selectedCode = $(e).children("td:nth(1)").text().trim();
				selectedName = $(e).children("td:nth(2)").text().trim();
				
				$('#dept_code').val(selectedCode);
				$('#dept_name').val(selectedName);
			}
		});
		
		$('#modal_div').modal("hide");
	});	
	
	$('#modal_div .modal-dialog').removeClass("modal-lg");

	$('#modal_div').modal("show");
}

$(function() {
	$("#repair_order_dept_btn").click(function() {
		initDeliveryDeptPopupUI();
	});
});